View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.impl; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 24 /*** 25 * A utility class 26 * 27 * @author <a href="mailto:david@carr.name">David Carr</a> 28 * @version $Revision: 1.3 $ 29 */ 30 class Util { 31 /*** 32 * Returns all interfaces that are implemented by this class and all of its super classes and interfaces 33 * 34 * @param c the Class to get the interfaces for 35 * @return an array of the Class objects representing the interfaces, or null if c is null 36 */ 37 static Class[] getAllInterfaces(Class c) { 38 if (c == null) 39 return null; 40 Class cur = c; 41 List al = new ArrayList(); 42 while (cur != null) { 43 Class[] ints = cur.getInterfaces(); 44 for (int i = 0; i < ints.length; i++) { 45 if (!al.contains(ints[i])) { 46 al.add(ints[i]); 47 Class[] subInts = getAllInterfaces(ints[i]); 48 //recursive call to handle sub-interfaces 49 for (int j = 0; j < subInts.length; j++) { 50 if (!al.contains(subInts[j])) { 51 al.add(subInts[j]); 52 } 53 } 54 } 55 } 56 cur = cur.getSuperclass(); 57 } 58 Class[] ret = new Class[al.size()]; 59 for (int i = 0; i < al.size(); i++) { 60 ret[i] = (Class) al.get(i); 61 } 62 return ret; 63 } 64 65 /*** 66 * Checks whether a class implements the specified interface (directly, by implementing a sub-interface, or by extending a class which implements it) 67 * 68 * @param c the class to check 69 * @param inter the interface to search for 70 * @return true if the specified interface is found, false if the arguments are invalid or the specified interface is not found 71 */ 72 public static boolean implementsInterface(Class c, Class inter) { 73 if (c == null) 74 return false; 75 Class[] interfaces = getAllInterfaces(c); 76 if (interfaces == null) 77 return false; 78 for (int i = 0; i < interfaces.length; i++) { 79 if (inter.equals(interfaces[i])) { 80 return true; 81 } 82 } 83 return false; 84 } 85 }

This page was automatically generated by Maven